<template>
  <div v-if="showPreview" id="preview">
    <!-- 关闭按钮 -->
    <div class="close-box">
      <el-button size="mini" icon="el-icon-close" circle @click="close" />
    </div>
    <span v-if="file.path">
      <!-- 图片 -->
      <div v-if="imgList.includes(file.path.match(/([.][^.]+)$/)[1])" style="min-height: 50vh;">
        <el-image class="showImg" style="width:100%;height:auto;" :src="file.path" />
      </div>
      <!-- 视频 -->
      <div v-if="videoList.includes(file.path.match(/([.][^.]+)$/)[1])" class="flex_around">
        <xgplayer v-if="file.open !== 'iframe'" :options="{url: file.path, fluid: true}" />
        <div v-if="file.open === 'iframe'">
          <iframe class="manual__iframe" :src="`js/pdf-ie9/web/viewer.html?file=${file.path}&pdf_toolbar_config=${JSON.stringify(pdf_toolbar_config)}`" :file-name="file.name" />
        </div>
      </div>
      <!-- 音频文件 -->
      <div v-if="audioList.includes(file.path.match(/([.][^.]+)$/)[1])">
        <audio :src="file.path" autoplay loop />
      </div>
      <!-- 压缩包 -->
      <div v-if="zipList.includes(file.path.match(/([.][^.]+)$/)[1])">
        <div class="formatError">{{ '当前文件格式暂不支持浏览' }}</div>
        <div class="formatErrorInfo">
          <el-link target="_blank" :href="file.path" :underline="false" style="margin-left:15px">
            <el-button type="danger" size="small">
              下载压缩包
            </el-button>
          </el-link>
        </div>
      </div>
      <!-- 文件 -->
      <div v-if="fileList.includes(file.path.match(/([.][^.]+)$/)[1])">
        <span v-if="file.path.match(/([.][^.]+)$/)[1] === '.pdf'">
          <iframe class="manual__iframe" :src="`js/pdf-ie9/web/viewer.html?file=${file.path}&pdf_toolbar_config=${JSON.stringify(pdf_toolbar_config)}`" :file-name="file.name" />
        </span>
        <span v-else>
          <div class="formatError">{{ '当前文件格式暂不支持浏览' }}</div>
          <div class="formatErrorInfo">
            <el-link target="_blank" :href="file.path" :underline="false" style="margin-left:15px">
              <el-button type="danger" size="small">
                下载文件
              </el-button>
            </el-link>
          </div>
        </span>
      </div>
    </span>
    <span v-else>
      <div class="pathNull">{{ '预览失败,文件参数缺失!' }}</div>
      <JsonViewer key="new" :value="file" :expand-depth="5" boxed sort expanded />
    </span>
  </div>
</template>
<script>
import JsonViewer from 'vue-json-viewer'
export default {
  name: 'SunPreviewFiles',
  components: { JsonViewer },
  props: {
    file: {
      type: Object,
      default() {
        return {}
      }
    },
    showPreview: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      imgList: ['.png', '.jpg', '.gif', '.jpeg', '.pic', '.tif', '.bmp'],
      videoList: ['.mp4', '.3gp', '.m4v', '.rmvb', '.avi', '.wmv'],
      audioList: ['.mp3', '.wma', '.wav', '.aif'],
      zipList: ['.rar', '.zip', '.7z'],
      fileList: ['.doc', '.pdf', '.docx'],
      page: 0, // 设置页码
      pageNum: 1,
      pageTotalNum: 1, // 总页数
      loadedRatio: 0 // 当前页面的加载进度，范围是0-1 ，等于1的时候代表当前页已经完全加载完成了
    }
  },
  computed: {
    pdf_toolbar_config() {
      return this.$project_config.project_config.pdf_toolbar_config ? this.$project_config.project_config.pdf_toolbar_config : []
    }
  },
  mounted() {

  },
  methods: {
    async close() {
      this.$parent.$parent.showPreview = false
    }
  }
}
</script>

<style>
.xgplayer-start{
  background: #5b9bd5;
}
</style>
<style lang="scss" scoped>
  .manual__iframe {
    width: 100%;
    height: 100vh;
    min-width: 400px;
  }
  /* 垂直居中 */
  .flex_center_align{ display: flex; flex-direction: row; align-items:center;}
  /** 水平右对齐 */
  .flex_end { display: flex; flex-direction: row; justify-content: flex-end; }
  .sun-previewFile ::v-deep {
    &.el-input__inner{
      border-top-right-radius: 0 !important;
      border-bottom-right-radius: 0!important;
    }
  }
  .close-box {
    position: absolute;right: -20px;top: -20px;width: 30px;height: 30px;z-index: 2;
  }
  .formatError{
    line-height: 300px;text-align: center;color: #888888;
  }
  .formatErrorInfo{
    display: flex; flex-direction: row; justify-content: center;
  }
  .pathNull {
    line-height: 50px;text-align: center;color: #888888;font-weight: bold;font-size: 16px;
  }
</style>
